package com.shop.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.MerchantGoods;
import com.shop.entity.SupplierSupplier;
import com.shop.mapper.MerchantGoodsMapper;
import com.shop.model.PageParam;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (MerchantGoods)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 09:48:30
 */
@Service
@AllArgsConstructor
public class MerchantGoodsService {
    private final MerchantGoodsMapper merchantGoodsMapper;


    /**
     * 通过商品类别id查询该商户下所有商品
     * @param categoryId
     * @return
     */
    public List<MerchantGoods> getGoodsListByCategoryId(String categoryId) {
        return merchantGoodsMapper.getGoodsListByCategoryId(Integer.parseInt(categoryId));
    }

    /**
     * 查询商品列表
     */
    public PageInfo<MerchantGoods> getGoodsList(PageParam<MerchantGoods> pageParam,Integer merchantId) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
//            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
//                PageHelper.orderBy(pageParam.getOrderParams()[i]);
//            }
            return new PageInfo<MerchantGoods>(merchantGoodsMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        } else {
            //查询列表
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
//        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
//            PageHelper.orderBy(pageParam.getOrderParams()[i]);
//        }
            List<MerchantGoods> merchantGoodsList=merchantGoodsMapper.getGoodsList(pageParam.getModel().getCategoryId(),merchantId);
            for (MerchantGoods merchantGoods : merchantGoodsList) {
                System.out.println(merchantGoods);
            }
            return new PageInfo<MerchantGoods>(merchantGoodsList);

        }
    }

    /**
     * 修改商品
     * @param merchantGoods
     * @return
     */
    public int updateGoods(MerchantGoods merchantGoods) {
        return merchantGoodsMapper.updateByGoodsId(merchantGoods);
    }

    /**
     * 新增商品
     * @param merchantGoods
     * @return
     */
    public int insertGoods(MerchantGoods merchantGoods) {
        return merchantGoodsMapper.insertGoods(merchantGoods);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    public int delGoods(Integer id) {
        return merchantGoodsMapper.deleteById(id);
    }

    /**
     * 根据类别查商品
     * @param categoryId
     * @param merchantId
     * @return
     */
    public List<MerchantGoods> getCategoryIdByMerchant(Integer categoryId, Integer merchantId) {
        return merchantGoodsMapper.getCategoryIdByMerchant(categoryId,merchantId);
    }
}
